// script.js
function uploadFiles() {
    const fileInput = document.getElementById('fileInput');
    const fileTableBody = document.querySelector('#fileTable tbody');
    const files = Array.from(fileInput.files);

    fileTableBody.innerHTML = ''; // 清空之前的文件列表

    files.forEach((file, index) => {
        const row = document.createElement('tr');

        const nameCell = document.createElement('td');
        nameCell.textContent = file.webkitRelativePath;

        const typeCell = document.createElement('td');
        typeCell.textContent = file.type || '未知类型';

        const progressCell = document.createElement('td');
        const progressBar = document.createElement('progress');
        progressBar.value = 0;
        progressBar.max = 100;
        progressCell.appendChild(progressBar);

        row.appendChild(nameCell);
        row.appendChild(typeCell);
        row.appendChild(progressCell);
        fileTableBody.appendChild(row);

        const formData = new FormData();
        formData.append('file', file);

        const xhr = new XMLHttpRequest();
        xhr.open('POST', '/index/uploadAction', true);

        xhr.upload.addEventListener('progress', (event) => {
            if (event.lengthComputable) {
                const percentComplete = (event.loaded / event.total) * 100;
                progressBar.value = percentComplete;
            }
        });

        xhr.addEventListener('load', () => {
            progressBar.value = 100;
        });

        xhr.addEventListener('error', () => {
            progressBar.value = 0;
            progressBar.innerText = '上传失败';
        });

        xhr.send(formData);
    });
}
